package com.xctech.yace.util;

import lombok.SneakyThrows;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    //double日期转化为date
    public static LocalDate duobleOfDate(Double value) {
        DecimalFormat decimalFormat = new DecimalFormat("#");
        String del = decimalFormat.format(value);
        LocalDate localDate = LocalDate.parse(del, DateTimeFormatter.BASIC_ISO_DATE);
        return localDate;
    }

    //获取当天日期
    public static String getTodayDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        return today;
    }

    //获取两个日期之间的days
    public static Long getBetweenDays(double startDay, double endDay) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date startDate = sdf.parse(new BigDecimal(startDay).toString());
        Date endDate = sdf.parse(new BigDecimal(endDay).toString());
        Long days = (endDate.getTime()-startDate.getTime())/(24*60*60*1000);
        return days;
    }

    //Double日期转换成String
    public static String changeStrDate(double date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return new BigDecimal(date).toString();
    }

    //获取n天前的日期
    public static String addStrDate(String date,Integer interval) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = sdf.parse(date);
        Calendar calendar  =  new GregorianCalendar();
        calendar.setTime(dt);
        calendar.add(calendar.DATE,interval);
        Date time = calendar.getTime();
        return sdf.format(time);
    }

    @SneakyThrows
    public static void main(String[] args) {
//        Long betweenDays = null;
//        try {
//            betweenDays = getBetweenDays(20210521d, 20210528d);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        System.out.println(betweenDays);
        String s = addStrDate(getTodayDate(), 30);
        System.out.println(s);
    }
}
